﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace SStuff.FlowControls
{
    /// <summary>
    /// Base class for the FlowItemsControl item containers (handle Selection logic and Visual States)
    /// </summary>
    [TemplateVisualState(GroupName = "SelectionStates", Name = "Unselected")]
    [TemplateVisualState(GroupName = "SelectionStates", Name = "Selected")]
    [TemplateVisualState(GroupName = "MouseStates", Name = "MouseOver")]
    [TemplateVisualState(GroupName = "MouseStates", Name = "MouseOut")]
    public class FlowItemBase : ContentControl
    {
        /// <summary>
        /// Logical Z index of the item (used for layout)
        /// </summary>
        public double RelativeZIndex { get; set; }

        /// <summary>
        /// Get or set the FlowItemsControl owning the current item
        /// </summary>
        public FlowItemsControlBase Owner
        {
            get { return (FlowItemsControlBase)GetValue(OwnerProperty); }
            set { SetValue(OwnerProperty, value); }
        }

        /// <summary>
        /// DependencyProperty for Owner
        /// </summary>
        public static readonly DependencyProperty OwnerProperty =
            DependencyProperty.Register("Owner", typeof(FlowItemsControlBase), typeof(FlowItemBase), new PropertyMetadata(null));

        bool _templateApplied;
        /// <summary>
        /// Set the initial visual state
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            UpdateSelectionVisualState();
            _templateApplied = true;
        }

        /// <summary>
        /// Update the Selection Visual State using the IsSelected property
        /// </summary>
        private void UpdateSelectionVisualState()
        {
            VisualStateManager.GoToState(this, IsSelected ? "Selected" : "Unselected", _templateApplied);
        }

        /// <summary>
        /// Get or set a flag indicating if the item is currently selected
        /// </summary>
        public bool IsSelected
        {
            get { return (bool)GetValue(IsSelectedProperty); }
            set { SetValue(IsSelectedProperty, value); }
        }

        /// <summary>
        /// Dependency property for IsSelected
        /// </summary>
        public static readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(FlowItemBase), new PropertyMetadata(false, (s, a) =>
                {
                    var asItem = s as FlowItemBase;
                    if (asItem != null)
                    {
                        if (asItem.IsSelected && asItem.Owner != null)
                        {
                            asItem.Owner.SelectedItem = asItem.Owner.ItemContainerGenerator.ItemFromContainer(asItem);
                        }
                        asItem.UpdateSelectionVisualState();
                    }
                }));

        /// <summary>
        /// Capture the mouse and set the IsSelected property to true
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            //e.Handled = true;
            //CaptureMouse();
            IsSelected = true;
        }

        /// <summary>
        /// Release the mouse capture
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonUp(e);
            //ReleaseMouseCapture();
        }

        /// <summary>
        /// Enter the MouseOver state
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseEnter(MouseEventArgs e)
        {
            base.OnMouseEnter(e);
            if (this.IsInMainVisualTree())
                VisualStateManager.GoToState(this, "MouseOver", true);
        }

        /// <summary>
        /// Enter the MouseOut state
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeave(MouseEventArgs e)
        {
            base.OnMouseLeave(e);
            if (this.IsInMainVisualTree())
                VisualStateManager.GoToState(this, "MouseOut", true);
        }


        /// <summary>
        /// Get or set the normalized offset for the current item
        /// </summary>
        public double NormalizedOffset
        {
            get { return (double)GetValue(NormalizedOffsetProperty); }
            set { SetValue(NormalizedOffsetProperty, value); }
        }

        /// <summary>
        /// DependencyProperty for NormalizedOffset
        /// </summary>
        public static readonly DependencyProperty NormalizedOffsetProperty =
            DependencyProperty.Register("NormalizedOffset", typeof(double), typeof(FlowItemBase), new PropertyMetadata(0.0));


        

    }
}
